package com.gqb.nio;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.FileChannel;

public class nioChannel {
	static private final byte message[] = { 83, 111, 109, 101, 32,  
        98, 121, 116, 101, 115, 46 };  
	public static void main(String[] args) throws Exception {
		FileInputStream fileIn=new FileInputStream("d:/test.txt");
		FileChannel channel=fileIn.getChannel();
		ByteBuffer buffer=ByteBuffer.allocate(1024);
		channel.read(buffer);
		buffer.flip();
		while(buffer.hasRemaining()){
			byte b=buffer.get();
			System.out.println((char)b);
		}
		fileIn.close();
		FileOutputStream fileout=new FileOutputStream("d:/test.txt");
		FileChannel channelout=fileout.getChannel();
		ByteBuffer buffer2=ByteBuffer.allocate(1024);
		for (int i = 0; i < message.length; i++) {
			buffer2.put(message[i]);
		}
		buffer2.flip();
		channelout.write(buffer2);
		fileout.close();
	}
}
